package com.wy.controller.wy;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.wy.constants.CommonValues;
import com.wy.utils.BaseResponseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: tracy
 * @Date: 2018/10/9 10:18
 * @Description:
 */
@RestController
@RequestMapping("/app")
public class AppDownLoadController {


    @Autowired
    private CommonValues commonValues;

    /**
     * @return
     * @throws IOException
     * @throws WriterException
     */
    @PostMapping("/getQR")
    public BaseResponseInfo getDownLoadAppQR(String path){
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = multiFormatWriter.encode(path + commonValues.getAppDownLoadPath(), BarcodeFormat.QR_CODE, 400, 400,hints);
            // 1、读取文件转换为字节数组
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            BufferedImage image = toBufferedImage(bitMatrix);

            ImageIO.write(image, "png", out);
            byte[] bytes = out.toByteArray();

            BASE64Encoder encoder = new BASE64Encoder();
            baseResponseInfo.data = encoder.encodeBuffer(bytes).trim();
            //2、输出图片到指定目录
//            File file1 = new File(path,"01.jpg");
//            MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baseResponseInfo;
    }


    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }

        return image;
    }




    /**
     * @param httpServletRequest
     * @param httpServletResponse
     * @return
     * @throws IOException
     * @throws WriterException
     */
    @PostMapping("/download")
    public BaseResponseInfo downloadQr(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, WriterException {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();


        return baseResponseInfo;
    }




    @RequestMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename="+new String( "楼栋信息.xls".getBytes("gb2312"), "ISO8859-1" ));
    }


    public static void main(String[] args) throws WriterException, IOException {
        String content = "http://127.0.0.1:89/app/estate.apk";
//            String path = commonValues.getBasePath();
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
        // 1、读取文件转换为字节数组
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedImage image = toBufferedImage(bitMatrix);

        ImageIO.write(image, "png", out);
        byte[] bytes = out.toByteArray();

        BASE64Encoder encoder = new BASE64Encoder();
        System.out.println(encoder.encodeBuffer(bytes).trim());
    }

}
